﻿using System;
using OpenTK;
using Mono.Rift.Core.Graphics;
using Mono.Rift.Platform.OSX;
using OpenTK.Graphics.OpenGL;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;
using otk = OpenTK;

namespace Mono.Rift.Samples.Box
{
	class MainClass
	{
		public static void Main (string[] args)
		{
			PlatformCoreOSXOpenTK.init ();

			box ();
			//manual ();
			//xboxController ();
		}

		private static void xboxController()
		{
		}

		private static void box()
		{
			var scene = new Scene ();
			var view = new Matrix4 ();
			var projection = new Matrix4 ();

			var platform = PlatformCore.create ();
			var renderer = platform.setupGraphics ();
			renderer.OnLoad = () => {

				/*				_boxModel = Model.createAxisFaceColorBox(
							-2.0f, 2.0f, new OculusVR.Rift.Mono.Platform.Render.OpenTK.Color(0, 0xAA, 0), 
							-1.0f, 1.0f, new OculusVR.Rift.Mono.Platform.Render.OpenTK.Color(0xAA, 0, 0),
							-1.0f, 1.0f, new OculusVR.Rift.Mono.Platform.Render.OpenTK.Color(0, 0, 0xAA));
*/
				var container = new Container ();
				/*				
				var downLine = new Model (PrimitiveTypes.Triangles);
				downLine.addLine (
					new Vertex (0.0f, -4.5f, 0.0f, 0xFFE08080),
					new Vertex (0.0f, 0.0f, 0.0f, 0xFFE08080));
				container.add (downLine);
*/
				var axes = new Model (PrimitiveTypes.Lines);
				axes.addLine (
					new Vertex (-8.0f, 0.0f, 0.0f, 0xFF40FF40),
					new Vertex (8.0f, 0.0f, 0.0f, 0xFF40FF40));
				/*				
				axes.addLine (
					new Vertex (7.6f, 0.4f, 0.4f, 0xFF40FF40),
					new Vertex (8.0f, 0.0f, 0.0f, 0xFF40FF40));
				axes.addLine (
					new Vertex (7.6f, -0.4f, -0.4f, 0xFF40FF40),
					new Vertex (8.0f, 0.0f, 0.0f, 0xFF40FF40));
					*/

				//scene.World.add (container);
				scene.World.add (axes);

				setView(ViewType.Perspective);
			};

			renderer.OnResize = () => {
			};

			renderer.UpdateFrame = () => {
			};
			renderer.RenderFrame = () => {
				/*
				renderer.setViewport (0, 0, 1280, 800);
				renderer.clear ();
				renderer.beginScene ();
				renderer.setProjection (projection);
				renderer.setDepthMode (true, true);
				scene.render (renderer, View);
				renderer.present ();
*/
				var state = GamePad.GetState(PlayerIndex.One);
				if (state.IsButtonDown(Buttons.A))
				{
				}


				/*				
						GL.Clear (ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

						GL.MatrixMode(MatrixMode.Projection);
						GL.LoadIdentity();
						GL.Ortho(-1.0, 1.0, -1.0, 1.0, 0.0, 4.0);

						GL.Begin(PrimitiveType.Triangles);
						GL.Color3(Color.MidnightBlue);
						GL.Vertex2(-1.0f, 1.0f);
						GL.Color3(Color.SpringGreen);
						GL.Vertex2(0.0f, -1.0f);
						GL.Color3(Color.Ivory);
						GL.Vertex2(1.0f, 1.0f);
						GL.End();

						gameWindow.SwapBuffers ();
						*/

			};
			renderer.run(30.0);
		}

		private static Matrix4 View;

		private static void setView(ViewType vt)
		{
			switch (vt) {
				case ViewType.Perspective:
					View = lookAtRH (
						new otk.Vector3 (5.0f, 4.0f, 10.0f),
						new otk.Vector3 (0.0f, 1.5f, 0.0f),
						new otk.Vector3 (0.0f, 1.0f, 0.0f));
					break;
			}
		}

		private static Matrix4 lookAtRH(otk.Vector3 eye, otk.Vector3 at, otk.Vector3 up)
		{
			var z = (eye - at).Normalized ();
			var x = otk.Vector3.Cross (up, z).Normalized ();
			var y = otk.Vector3.Cross (z, x);
			var dx = -(otk.Vector3.Dot (x, eye));
			var dy = -(otk.Vector3.Dot (y, eye));
			var dz = -(otk.Vector3.Dot (z, eye));
			var m = new Matrix4 (
				x.X, x.Y, x.Z, dx,
				y.X, y.Y, y.Z, dy,
				z.X, z.Y, z.Z, dz,
				0.0f, 0.0f, 0.0f, 1.0f);
			return m;
		}

		public static void manual()
		{
			var platform = PlatformCore.create ();
			var renderer = platform.setupGraphics () as OpenTKOSXRenderDevice;
			renderer.OnLoad = () =>
			{
			};
			var font = new System.Drawing.Font ("Arial", 20);

			renderer.RenderFrame = () =>
			{
				//renderer.setViewport (0, 0, 1280, 800);
				renderer.clear (0.0f, 0.0f, 0.0f, 1.0f);
				//renderer.beginScene ();
				//renderer.setProjection (projection);
				//renderer.setDepthMode (true, true);
				//scene.render (renderer, View);

				GL.MatrixMode(MatrixMode.Projection);
				GL.LoadIdentity();
				GL.Ortho(-1.0, 1.0, -1.0, 1.0, 0.0, 4.0);
				/*
				GL.Begin(PrimitiveType.Triangles);
				GL.Color4(Color4.MidnightBlue);
				GL.Vertex2(-1.0f, 1.0f);
				GL.Color4(Color4.SpringGreen);
				GL.Vertex2(0.0f, -1.0f);
				GL.Color4(Color4.Ivory);
				GL.Vertex2(1.0f, 1.0f);
				GL.End();
				*/

				GL.Begin(PrimitiveType.Lines);
				GL.Color4(OpenTK.Graphics.Color4.Red);
				GL.Vertex3(-0.9, 0, 0);
				GL.Vertex3(0.9, 0, 0);
				var printer = new OpenTK.Graphics.TextPrinter();
				printer.Print("HI", font, System.Drawing.Color.White);

				GL.End();

				renderer.GameWindow.SwapBuffers();

			};
			renderer.run (30.0);
		}
	}
}
